package com.lbiuse.cwc.init;

import com.lbiuse.utils.propertyUtils.WxConfigUtils;
import com.lbiuse.wx.AccountCache;
import com.lbiuse.wx.DefaultWechatCache;
import com.lbiuse.wx.api.BaseAPI;
import com.lbiuse.wx.api.MessageAPI;
import com.lbiuse.wx.api.WeChatAPI;
import com.lbiuse.wx.bean.Account;
import com.lbiuse.wx.bean.message.send.Text;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServlet;

/**
 * @author libing
 * @version 1.0
 * @Description:
 * @Copyright: Copyright (c) 2015
 * @Company: pinchehui
 * @Date 2015/9/6
 */
public class InitParams extends HttpServlet {
    private static final Logger logger = Logger.getLogger(InitParams.class);

    public InitParams() {
        super();
    }

    @Override
    public void destroy() {
        super.destroy();
    }

    @Override
    public void init() {
        logger.info("initial begin.");;
        AccountCache.put(new Account(WxConfigUtils.getAppId(),
                WxConfigUtils.getSecrect()));
        String accessTokenServer = WxConfigUtils
                .getString("wx.accessToken.server");
        if (accessTokenServer != null && accessTokenServer.length() > 0) {
            BaseAPI.ACCESS_TOKEN_URL = accessTokenServer;
        }
        System.out.println("AccessToken服务:" + BaseAPI.ACCESS_TOKEN_URL);

        Integer cacheTime = WxConfigUtils.getInteger("wx.accessToken.cacheTime");
        if (cacheTime != null) {
            DefaultWechatCache.ACCESSTOKEN_CACHE_TIME = cacheTime;
        }
        WeChatAPI api= WeChatAPI.getAPI(WxConfigUtils.getAppId());
        String openId = WxConfigUtils.getString("debug.openId");
        System.out.printf(MessageAPI.sendCustomMessage(WxConfigUtils.getAppId(), new Text("ssss"),openId).toString()); ;

        System.out.printf(api.getMenu().toString());
        System.out.println("微信初始化完毕");
        logger.info("initial end.");
    }
}
